package com.ygl.service.impl;

import com.ygl.api.service.ItemService;
import com.ygl.common.api.BaseApiService;
import com.ygl.dao.ItemCatDao;
import com.ygl.dao.ItemDao;
import com.ygl.entity.ItemCatEntity;
import com.ygl.entity.ItemEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author： ygl
 * @date： 2019/02/04
 * @Description：
 */
@RestController
public class ItemServiceImpl extends BaseApiService implements ItemService {
    @Autowired
    private ItemDao itemDao;
    @Autowired
    private ItemCatDao itemCatDao;

    @RequestMapping("/getIndexItem")
    public Map<String, Object> getIndexItem() {
        // 查询所有的类型
        List<ItemCatEntity> listItemCat = itemCatDao.allItemCat();
        Map<String, Object> reuslt = new HashMap<String, Object>();
        for (ItemCatEntity itemCatEntity : listItemCat) {
            Long id = itemCatEntity.getId();
            String name = itemCatEntity.getName();
            List<ItemEntity> listItem = itemDao.getIndexItem(id);
            if (!(listItem.isEmpty() && listItem.size() <= 0)) {
                reuslt.put(name, listItem);
            }

        }
        return setResponseSuccessData(reuslt);
    }

    @Override
    public Map<String, Object> geItem(@RequestParam("id") Long id) {
        ItemEntity item = itemDao.getItem(id);
        if(item==null){
            return setResponseError("没有查询到结果");
        }
        return setResponseSuccessData(item);
    }

}
